        org &cd40
 
         
reg4_max_value    equ 38
reg4_first_value  equ 20
reg4_second_value equ 17
reg6_first_value  equ 21
reg6_second_value equ 14
reg7_first_value  equ 127
reg7_second_value equ 15

        di 

;        ld   hl,(&38)            ; save interrupt
;        ld   (saveinterrupt+1),hl
        ld   hl,&c9fb            ; set new interrupt
        ld   (&38),hl

        ld   bc,&7f00+%10011100  ; Set Interrupt and Upper/Lower rom area disable
        out (c),c

	call wait_vsync
        
        ld hl,table_couleurs_0
	ld a,17
	call set_color

;        ld bc,&7fc4              ; save bank 0/0 to 1/0
;        out (c),c
;        ld hl,&60
;        ld de,&4000
;        ld bc,&3fa0
;        ldir

	ld hl,table_ecr_rupture
	ld a,7
	call set_rupture

        ld bc,&7fc5              ; scr11 from bank 1/1 to 0/0
        out (c),c
        ld hl,&4060
        ld de,&60
        ld bc,&3fa0
        ldir

;        ld hl,&8000		; save bank 0/2 to 1/1
;        ld de,&4000
;        ld bc,&4000
;        ldir

        ld bc,&7fc7              ; copy src12 from bank 1/3 to 0/2
        out (c),c

        ld hl,&4000
        ld de,&8000
        ld bc,&4000
        ldir
        
        ld bc,&7fc6		; init the starkos music
        out (c),c
	ld de,&4A00
	call &4000

	ld l,&30
wait_for_screensync
	call wait_vsync
	ei
	halt
	halt
	di
	dec l
	jr nz,wait_for_screensync

dothejob:
	call wait_vsync

	ei
	halt
	di

mode_and_swap_scr
        ld a,&01
        cp &00
        jr nz,mode0

	ld a,&20
	ld (table_ecr_rupture2_swap+1),a

	xor a
	ld (table_ecr_rupture1_swap+1),a

	inc a
        jr mode1 
mode0
        ld a,&30
	ld (table_ecr_rupture2_swap+1),a

	ld a,&10
	ld (table_ecr_rupture1_swap+1),a

	xor a 
	defs 2,0
mode1
        ld (mode_and_swap_scr+1),a
        
        add %10011100
	
	ld b,&7f
	out (c),a
        
first_screen_address
	ld hl,table_ecr_rupture1_swap
	ld a,2
	call set_rupture

	ei
	halt
	halt
	di

	ld hl,table_ecr_rupture1
	ld a,5
	call set_rupture

	ei
	halt
	halt
;	halt
	di

	ld hl,table_ecr_rupture2
	ld a,3
	call set_rupture

        ld hl,table_couleurs_mode_0
	ld a,17
	call set_color

	call &4000+3		; play music

	ld a,(&4000+&b)
	cp 0
	jr z,dothejob

greetings
	ld bc,&7f00+%10011101 ; mode 1
	out (c),c

	call wait_vsync

	ld hl,table_ecr_greetings
	ld a,8
	call set_rupture

	call &4000+3		; play music

	jp compte_greetings

greetings_sync
	call wait_vsync

	ei
	halt
	halt
	di

        ld bc,&7fc6              
        out (c),c

	call &4000+3		; play music

	ld a,(&4000+&b)
	cp 0
	jr z,swap_sprite_greetings

compte_greetings
	ld a,0
	inc a
	cp 5
	jr z,greetings_end
	ld (compte_greetings+1),a

        ld bc,&7fc4
        out (c),c

	ld hl,table_couleurs_0
	ld a,4
	call set_color

	call write_sprite_greets

set_table_couleurs_greetings
	ld hl,table_couleurs_greetings
	ld a,4
	call set_color
	ld (set_table_couleurs_greetings+1),hl

swap_sprite_greetings
	ld a,1
	cp &00
	jr nz,ecr_greetings_swap1

	ld hl,table_ecr_greetings_swap1
	ld a,1
	jr ecr_greetings_swap2

ecr_greetings_swap1
	ld hl,table_ecr_greetings_swap2
	xor a

ecr_greetings_swap2
	ld (swap_sprite_greetings+1),a

	ld a,1
	call set_rupture

        jp greetings_sync

greetings_end   
;        di
;        ld bc,&7fc4     ; restore bank 0/1 from 1/0
;        out (c),c
;        ld hl,&4000
;        ld de,&0060
;        ld bc,&3fa0
;        ldir
;
;        ld bc,&7fc5    ; restore bank 0/2 from 1/2
;        out (c),c
;        ld hl,&4000
;        ld de,&8000
;        ld bc,&4000
;        ldir

;saveinterrupt:
;        ld  hl,0
;        ld  (#38),hl
;        ei

;	call wait_vsync

	ld hl,table_couleurs_0
	ld a,4
	call set_color

        jp greetings_end
;        ret

wait_vsync
        ld b,&f5
synchro : 
        in a,(c)
        rra
        jr nc,synchro
	ret

set_rupture
	ld b,&bd
set_crtc_regs
	outi
	inc b
	inc b
	outi
	dec a
	jr nz,set_crtc_regs
	ret

set_color
        ld bc,&7f00

selectcolor:
	out (c),c
	inc b
	outi
        inc c
        cp c
        jr nz,selectcolor
        ret

write_sprite_greets
	ld hl,&4000
	
	ld a,&c0
	ld (write_sprite_adr+1),a
	call write_sprite_greets1

	ld a,&c1
	ld (write_sprite_adr+1),a

write_sprite_greets1
	xor a
	ld (write_1_sprite_line+1),a
write_sprite
	ld (write_sprite_line_number+1),a
	xor a
write_sprite_adr
	ld d,&c0
	ld e,a
	ld a,d
	ld (save_d+1),a
	ld a,e

write_1_sprite_line
        ld e,&00
        ld bc,&40
        ldir

	ld c,a

save_d	ld a,0
	add &8
	ld d,a
	ld (save_d+1),a

	ld a,c
	inc a
	cp 8
	jr nz,write_1_sprite_line

	ld a,e
	ld (write_1_sprite_line+1),a

write_sprite_line_number
	ld a,0
	inc a
	cp 4
	jr nz,write_sprite

	ld (write_sprite_greets+1),hl
	ret

table_ecr_rupture
	defb 01,48
	defb 02,50
	defb 03,&8e
	defb 05,00
	defb 06,00
	defb 07,16
	defb 09,07

table_ecr_rupture1
	defb 04,reg4_first_value
	defb 06,reg6_first_value
        defb 07,reg7_first_value
table_ecr_rupture2_swap
	defb 12,&00
	defb 13,&00
table_ecr_rupture2
        defb 04,reg4_second_value
	defb 06,reg6_second_value
        defb 07,reg7_second_value


table_ecr_rupture1_swap
	defb 12,&00
	defb 13,&00


table_ecr_greetings
	defb 01,32 ;40
	defb 02,42 ;46
	defb 04,reg4_max_value
	defb 05,00
	defb 06,04 ;15
	defb 07,20 ;30
	defb 12,&30
	defb 13,&00

table_ecr_greetings_swap1
;	defb 12,&30
	defb 13,&00
table_ecr_greetings_swap2
;	defb 12,&30
	defb 13,&80

table_couleurs_mode_0
	defb &54,&5c,&5e,&43,&45,&58,&4f,&5d,&44,&5f,&46,&59,&4c,&47,&4b,&40,&54
table_couleurs_greetings
	defb &54,&4b,&4a,&57
	defb &54,&4b,&45,&52
	defb &54,&4b,&4d,&56
	defb &54,&4b,&4e,&5d
table_couleurs_0
        defs 16,&54
